import cv2
hongjun = cv2.imread("../images/cyj.jpg")
logo = cv2.imread("../images/logo.jpg")

def imgLogo(logo,hongjun):
    # 步骤 1：处理 logo
    # 1.1 对 Logo 进行灰度处理
    logo_gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
    # 1.2 对灰度图进行二值化、非运算，得到两个 mask
    ret, mask1 = cv2.threshold(logo_gray, 200, 255, cv2.THRESH_BINARY)
    # 二值化，>=200，设置为255，其他设置为0 mask1
    mask2 = cv2.bitwise_not(mask1)  # 对 mask1 进行非操作，得到 mask2
    # cv2.imshow("logo", logo)
    # cv2.imshow("logo_gray", logo_gray)
    # cv2.imshow("mask1", mask1)
    # cv2.imshow("mask2", mask2)

    # 步骤 2：按 LOGO 大小截取目标图像的 ROI
    row1, col1, channel = logo.shape
    row2, col2, channel = hongjun.shape
    hongjun_roi = hongjun[0:row1, col2-col1:col2].copy()
    # cv2.imshow("hongjun_roi", hongjun_roi)

    # 步骤 3：将 mask1 和 mask2 分别与 hongjun 进行与运算
    # hongjun_roi 中对应掩模 mask1 的部分被保留，其他部分被设置为 0。用于将 hongjun_roi 中的特定区域提取出来。
    res1 = cv2.bitwise_and(hongjun_roi, hongjun_roi, mask=mask1)
    res2 = cv2.bitwise_and(logo, logo, mask=mask2)
    res3 = cv2.add(res1, res2)

    hongjun[:row1, col2-col1:col2] = res3[:,:]  # 将修改后的图片头放入图片中
    # :row1：表示从第0行到第row1行（不包括row1）。
    cv2.imshow("rest1", res1)
    cv2.imshow("rest2", res2)
    cv2.imshow("rest3", res3)
    cv2.imshow("hongjun", hongjun)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

imgLogo(logo,hongjun)

# import cv2
# hongjun = cv2.imread("../images/cyj.jpg")
# logo = cv2.imread("../images/logo.jpg")
# def imgLogo(logo,hongjun):
# #步骤1；处理logo
# #1.1对logo进行灰度处理
#     logo_gray=cv2.cvtColor(logo,cv2.COLOR_BGRA2GRAY)
#     #对灰度图1进行二值化,非运算，得到两个mask
#     ret,mask1 = cv2.threshold(logo_gray,200,255,cv2.THRESH_BINARY)
#     #二值化，>=200，设置为255，其他设置为0mask1
#     mask2 = cv2.bitwise_not(mask1)#对mask1进行非操作，得到mask2
#     #cv2.imshow("logo",logo)
#     #cv2.imshow("logo_grar",logo_grar)
#     #cv2.imshow("mask1",mask1)
#     #cv2.imshow("mask2",mask2)
#     row1,col1,channel = logo.shape
#     row2,col2,channel = hongjun.shape
#     hongjun_roi = hongjun[0:row1,col2-col1:col2].copy()
#     #cv2.imshow("hongjun_roi,hongjun_roi,mask=mask1)
#     #步骤三：将mask1和mask2分别与hongjun进行运算
#     #hongjun_roi中对应淹模mask1的部分被保留，其他部分被设置为0。用于将hongjun_roi中的特定区域提取出来。
#     res1=cv2.bitwise_and(hongjun_roi,hongjun_roi,mask=mask2)
#     res2=cv2.bitwise_and(logo,logo,mask=mask2)
#     res3=cv2.add(res1,res2)
#
#     hongjun[:row1,col2-col1:col2]=res3[:,:]
#     cv2.imshow("res1",res1)
#     cv2.imshow("res2",res2)
#     cv2.imshow("res3",res3)
#     cv2.imshow("hongjun",hongjun)
#     cv2.waitKey(0)
#     cv2.destroyAllWindows()
# imgLogo(logo,hongjun)